/**
 *  *2010-4-25 下午03:11:14  add 
 */
package com.base.myproject.client.busi.service.gcode;

import java.util.Date;
import java.util.HashMap;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.jdbc.StoreInfo;
import com.base.myproject.client.system.BaseResource;
import com.base.myproject.client.system.Env;
import com.base.myproject.client.system.SystemString;
import com.base.myproject.client.tools.CommandSyncContainer;
import com.base.myproject.client.tools.CommandSyncReturnObject;
import com.base.myproject.client.tools.CommandSyncsql;
import com.base.myproject.client.tools.GreetingService;
import com.base.myproject.client.tools.GreetingServiceAsync;
import com.base.myproject.client.ui.BaseFormPanel;
import com.base.myproject.client.ui.celleditor.BaseSimpleCombox;
import com.extjs.gxt.ui.client.Style.Orientation;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.Dialog;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.form.ComboBox;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.layout.RowData;
import com.extjs.gxt.ui.client.widget.layout.RowLayout;
import com.extjs.gxt.ui.client.widget.layout.TableData;
import com.extjs.gxt.ui.client.widget.layout.TableLayout;
import com.google.gwt.core.client.GWT;
import com.google.gwt.i18n.client.DateTimeFormat;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.AbstractImagePrototype;
import com.google.gwt.user.client.ui.Label;

/**
 * 添加商品
 * 添加商品到商品表gcode
 * @author Administrator
 *2010-4-25
 * javazgw@gmail.com
 */
public class NewGcode implements Listener<BaseEvent> {
	//国际化
	SystemString systemstring = GWT.create(SystemString.class);
	Dialog d = new Dialog();
	BaseFormPanel w;
	
	
	TextField<String> cgicode_t = new TextField<String>();
	TextField<String> cgname_t = new TextField<String>();
	TextField<String> modeltype_t = new TextField<String>();
	
	BaseSimpleCombox gcode_t = new BaseSimpleCombox("gname","gcode","");
	
	public NewGcode()
	{
		init();
		
	}

	private void init() {
		
		initWidget();
	}

	private void initWidget() {
		initmainpanel();
		
		d.setLayout(new RowLayout(Orientation.HORIZONTAL));

		d.setButtons(Dialog.YESNO);
		d.getButtonById(Dialog.YES).setText("添加商品");
		d.getButtonById(Dialog.NO).setText("关闭");
		d.getButtonById(Dialog.YES).setIcon(
				AbstractImagePrototype
						.create(BaseResource.INSTANCE.contact24()));

		d.getButtonById(Dialog.YES).addListener(Events.Select, this);
		d.getButtonById(Dialog.NO).addListener(Events.Select, this);

		d.add(w, new RowData(1, 1));
		d.setSize(585, 300);
		//store._LoadComplete();
		store._LoadComplete();
	}
	
	private void setdefault() {

		DateTimeFormat activeFormat = DateTimeFormat
				.getFormat("yyyy-M-d H:mm:ss"); //$NON-NLS-1$
		activeFormat.format(new Date());

//		store.addDefault("ccodetype", "1");
		store.addDefault("bcode", Env.getEnvbcode());
//		store.addDefault("createbname", Env.getEnvname());
		store.addDefault("createdate", activeFormat.format(new Date()));

	}

	BaseStore<BusiObject> store;
/**
 * 获得数据
 * @return
 */

	BaseStore<BusiObject> getStore() {
		store = new BaseStore<BusiObject>();
		// store.addBaseStoreListener( EventType.ROW_ADDED,
		// getBasestoreListener());

		ModelType type = new ModelType();
		type.setRoot("records"); //$NON-NLS-1$
		type.setRecordName("record"); //$NON-NLS-1$
		type.setTotalName("totle"); //$NON-NLS-1$
		type.addField("icode"); //$NON-NLS-1$
		type.addField("cgicode"); //$NON-NLS-1$
		type.addField("cgname"); //$NON-NLS-1$
		type.addField("modeltype"); //$NON-NLS-1$
		type.addField("gcode"); //$NON-NLS-1$



		StoreInfo info = new StoreInfo();
		info.setTablename("cgcode"); //$NON-NLS-1$
		info.setKeyname(new String[] { "icode" }); //$NON-NLS-1$
		store.setType(type);
		store.setStoreinfo(info);
		setdefault();
		HashMap hm = new HashMap();

		BusiObject modeldata = new BusiObject(hm);

		store.add(modeldata, true, false);

		return store;
	}
	void initmainpanel() {
		TableLayout tl = new TableLayout(2);
		tl.setWidth("600");
		
		TableData td = new TableData();
		td.setColspan(1);
		// td.setWidth("2000");
		td.setMargin(100);
		td.setPadding(5);
		td.setRowspan(1);
		
		w = new BaseFormPanel(getStore());
		w.setLayout(tl);
		w.setHeaderVisible(false);
		Label cgicode_l = new Label(systemstring.barcode());
		cgicode_l.setWidth("80"); 
		
		Label  cgname_l= new Label(systemstring.cgname());
		cgname_l.setWidth("50"); 
		
		Label modeltype_l = new Label(systemstring.modeltype());
		modeltype_l.setWidth("50"); 
		
		Label gcode_l = new Label(systemstring.gcode());
		gcode_l.setWidth("50"); 
		
		
//		cgicode_t = new TextField<String>();
//		cgname_t = new TextField<String>();
//		modeltype_t = new TextField<String>();
//		gcode_t = new TextField<String>();
		
		cgicode_t.setWidth("250"); 
		cgname_t.setWidth("250"); 
		modeltype_t.setWidth("250"); 
		gcode_t.getCombox().setWidth("250"); 
		
		
		cgicode_t.setName("cgicode");
		cgname_t.setName("cgname");
		modeltype_t.setName("modeltype");
		gcode_t.getCombox().setName("gcode");
		
		w.add(cgicode_l, td);
		w.add(cgicode_t, td);
		w.add(cgname_l, td);
		w.add(cgname_t, td);
		w.add(modeltype_l, td);
		w.add(modeltype_t, td);
		w.add(gcode_l, td);
		w.add(gcode_t.getCombox(), td);
		
		w.inited();
	}
	/**
	 * 显示窗口
	 */
	public void show()
	{
		d.show();
	}
	/**
	 * 关闭窗口
	 */
	
	public void close()
	{
		
	}
	/**
	 * 允许添加后执行
	 */
	public void OK()
	{
		
	}
	
	 int sync = 0;
		/* (non-Javadoc)
		 * @see com.extjs.gxt.ui.client.event.Listener#handleEvent(com.extjs.gxt.ui.client.event.BaseEvent)
		 */
		public void handleEvent(BaseEvent be) {
			
			sync++;
			try {
				if (sync == 1) {
					if (be.getType() == Events.Select) {
						if (be.getSource() == d.getButtonById(Dialog.YES)) { 
							 CommandSyncContainer list  = new CommandSyncContainer();
							 CommandSyncsql commandsql = new CommandSyncsql();
							 
							// 保存
							commandsql.getV().addAll(store._trySave());
							list.add(commandsql);
							GreetingServiceAsync greetingService = GWT
							.create(GreetingService.class);
							
							greetingService.SendCommandSync(list,
									new AsyncCallback<CommandSyncReturnObject>() {
										public void onFailure(Throwable caught) {
											Window.alert("网络连接不稳定，请稍后重试！");
										}

										public void onSuccess(
												CommandSyncReturnObject cyro) {

											System.out.println("成功："
													+ cyro.getMessage() + cyro.isB()
													+ cyro.getRetrunstr());
											if (cyro.isB()) {
												Info.display("", "保存成功", "");
												//close();
											} else {
												MessageBox.alert("错误", "保存错误！"
														+ cyro.getMessage(), null);
											}
										}
									});
							 
						
						//	d.getButtonById(Dialog.YES).setEnabled(false);
							
						}
					}
				}
			}
			finally
			{
				sync--;
			}
			
		}

}
